package com.ideal.gpt.app.docqajob.localjob;

import java.io.File;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.ideal.gpt.ability.docqa.splitter.ResponseSplitter;
import com.ideal.gpt.ability.docqa.splitter.ResponseSplitter.ChoiceResult;
import com.lzwork.utility.collection.CollUtility;
import com.lzwork.utility.officeparser.ExcelLoader;
import com.lzwork.utility.officeparser.ExcelSaver;
import com.lzwork.utility.pair.Pair;
import com.lzwork.utility.str.StrUtility;

/**
 * @author LiZheng
 * @date 2023年10月30日 下午4:16:44
 */

public class SplitChoice extends ExcelSaver
{
	public static void main(String[] args)
	{
		File input = new File("V:/doc qa/train_format_choice.xlsx");

		Workbook wb = ExcelLoader.loadWorkBook(input);

		for (Sheet sheet : wb)
		{
			inner: for (Row row : sheet)
			{
				if (row.getRowNum() == 0)
					continue inner;

				String state = ExcelLoader.getCellData(row.getCell(1)).getString();
				if (ChoiceResult.S_FULL.equals(state))
					continue inner;

				if (ChoiceResult.S_SIMPLE.equals(state))
				{
					String qF = ExcelLoader.getCellData(row.getCell(2)).getString();

					Pair<String, List<String>> detailSplit = ResponseSplitter.splitChoiceDetail(qF);

					List<String> choices = detailSplit.second();
					if (!CollUtility.hasData(choices))
						continue inner;

					Cell cell = pickCellAt(row, 3);
					cell.setCellValue(detailSplit.first());

					cell = pickCellAt(row, 1);
					cell.setCellValue(ChoiceResult.S_FULL);

					writeLine(row, 5, choices);
					continue inner;
				}

				String data = ExcelLoader.getCellData(row.getCell(0)).getString();
				if (!StrUtility.stringValid(data))
					continue inner;

				List<String> toWrite;

				ChoiceResult content = ResponseSplitter.splitChoice(data, false);
				toWrite = content.toData(true);

				writeLine(row, 1, toWrite);
			}
		}

		saveXlsFile(wb, input, true);
	}
}
